package com.google.cbmgraph.data;

import java.util.ArrayList;

import android.util.Log;

public class GraphData {
	
	private static ArrayList<int[]> datapoints;
/**
 * This populates the data points from the GetData class's grabData function, it pulls data form the
 * database. In cases where the internet is inaccessible,f or testing purposes, I have it populate with 
 * dummy data
 */
	public GraphData(){
		datapoints = new ArrayList <int[]>();
		// try getting the data and parsing it
		// if that's not possible, use the 
		try {
			String xmlData = GetData.grabData();
			datapoints = GetData.parseXML(xmlData);
			for(int i =0; i<datapoints.size(); i++){
				Log.d("Populating data", "Data point for "+ i + " = "+ datapoints.get(i)[0]+", "+datapoints.get(i)[1]);
			}
		} catch (Exception e) {
			int data[][]={ {0,0}, {1,1}, {2,2}, {93,3} , {93, 4}, {80,6}, {75,7}, {72, 8}, {60, 9}, {50,11}, {20,12}};
			for (int i = 0; i<data.length; i++){
				int mypoint[] = new int[2];
				mypoint[0]= data[i][0];
				mypoint[1]= data[i][1];
				datapoints.add(mypoint);
			}
		}
	}
	/** 
	 * just an accessor
	 * @return
	 */
	
	public ArrayList<int[]> getData(){
		return datapoints;
	}
/**
 * A helper function returns an array with min and maximum values
 * @return
 */
	public static int[] getMinandMax(){
		int xmin, ymin, xmax, ymax;
		xmin = Integer.MAX_VALUE;
		ymin = Integer.MAX_VALUE;
		xmax = Integer.MIN_VALUE;
		ymax = Integer.MIN_VALUE;
		for (int i = 0; i < datapoints.size(); i++){
			int x = datapoints.get(i)[0];
			int y = datapoints.get(i)[1];
			if (x < xmin)
				xmin = x;
			if (x > xmax)
				xmax = x;
			if (y< ymin)
				ymin = y;
			if (y > ymax)
				ymax = y;
		}
		int results[] =  {(int) xmin, (int) xmax, (int) ymin, (int) ymax};
		return results;
	}
}
